﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Mail;

namespace Having.Site.Base
{
    /// <summary>
    /// .Net邮件发送程序（.net自带的）
    /// </summary>
    public class Email
    {
        /// <summary>
        /// 邮件发送
        /// </summary>
        public static Result SendEmail(EMailServer ems, EMailBody emb)
        {
            Result rs = new Result();
            System.Net.Mail.SmtpClient SMTP = new System.Net.Mail.SmtpClient(ems.Host,ems.Port);
            SMTP.Timeout = 200000;
            MailMessage message = new MailMessage();
            message.From = new System.Net.Mail.MailAddress(ems.UserName);
            message.Body = emb.Body;//收信人的内容
            if (emb.BCC != null && emb.BCC.Count>0)
            {
                foreach (string str in emb.BCC)
                {
                    message.Bcc.Add(str);//密件抄送人的地址
                }
            }
            if (emb.CC != null && emb.CC.Count > 0)
            {
                foreach (string str in emb.CC)
                {
                    message.CC.Add(str);//抄送人的地址
                }
            }
            message.IsBodyHtml = emb.IsBodyHtml;//邮件正文是否为html格式 默认是
            message.Priority = System.Net.Mail.MailPriority.Normal;//邮件优先默认为高
            message.Subject = emb.Subject;//主题
            foreach (string str in emb.EMailTo)
            {
                message.To.Add(str);//收信人的地址
            }
            message.BodyEncoding = System.Text.Encoding.GetEncoding("utf-8");
            message.Sender = new System.Net.Mail.MailAddress(ems.UserName);
            NetworkCredential credential = new NetworkCredential(ems.UserName,ems.PassWord);
            SMTP.Credentials = credential;
            SMTP.EnableSsl = ems.EnableSsl;
            SMTP.DeliveryMethod = SmtpDeliveryMethod.Network;
            try
            {
                SMTP.Send(message);
                rs.Code = true;
            }
            catch (ArgumentNullException)
            {
                rs.Code = false;
                rs.Msg = "没有填写收信人或者发信人";
            }
            catch (SmtpException sex)
            {
                rs.Code = false;
                rs.Msg = "邮件服务器/用户名/密码出错/邮箱配置出错";
            }
            finally
            {
                credential = null;
                SMTP = null;
                message.Dispose();
            }
            return rs;
        }
    }

    /// <summary>
    /// 发邮件服务器设置
    /// </summary>
    public class EMailServer
    {
        /// <summary>
        /// smtp的服务器名称
        /// </summary>
        public string Host { set; get; }
        /// <summary>
        /// 端口号
        /// </summary>
        public int Port { set; get; }

        /// <summary>
        /// 用户名（邮箱地址）
        /// </summary>
        public string UserName { set; get; }

        /// <summary>
        /// 邮箱登录的密码或者授权码
        /// </summary>
        public string PassWord { set; get; }

        /// <summary>
        /// 是否使用SSL/TLS/STARTTLS
        /// </summary>
        public bool EnableSsl { set; get; }

        /// <summary>
        /// 初始
        /// </summary>
        public EMailServer(){}

        /// <summary>
        /// 初始
        /// </summary>
        /// <param name="_Host">smtp的服务器名称(smtp.xx.com)</param>
        /// <param name="_Port">端口号,默认是25</param>
        /// <param name="_UserName">用户名（邮箱地址）</param>
        /// <param name="_PassWord">邮箱登录的密码或者授权码</param>
        /// <param name="_EnableSsl">是否使用SSL/TLS/STARTTLS,默认false</param>
        public EMailServer(string _Host,  string _UserName, string _PassWord,bool _EnableSsl=false, int _Port=25) {
            this.EnableSsl = _EnableSsl;
            this.Host = _Host;
            this.PassWord = _PassWord;
            this.Port = _Port;
            this.UserName = _UserName;
        }
    }

    /// <summary>
    /// 邮件信息
    /// </summary>
    public class EMailBody {
        /// <summary>
        /// 收件人集合
        /// </summary>
        public List<string> EMailTo { set; get; }
        /// <summary>
        /// 邮件内容
        /// </summary>
        public string Body { set; get; }
        /// <summary>
        /// 邮件内容是否使用html方式（默认是）
        /// </summary>
        public bool IsBodyHtml { set; get; }

        /// <summary>
        /// 邮件主题
        /// </summary>
        public string Subject { set; get; }
        /// <summary>
        /// 密送人地址
        /// </summary>
        public List<string> BCC { set; get; }
        /// <summary>
        /// 抄送人地址
        /// </summary>
        public List<string> CC { set; get; }

        public EMailBody() {
            this.IsBodyHtml = true;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="_EMailTo">收件人</param>
        /// <param name="_Subject">邮件主题</param>
        /// <param name="_Body">邮件内容</param>
        /// <param name="_BCC">密送人地址</param>
        /// <param name="_CC">抄送人地址</param>
        /// <param name="_IsBodyHtml">邮件内容是否使用html方式（默认是）</param>
        public EMailBody(List<string> _EMailTo, string _Subject,string _Body, List<string> _BCC=null, List<string> _CC=null, bool _IsBodyHtml=true)
        {
            this.IsBodyHtml = _IsBodyHtml;
            this.BCC = _BCC;
            this.Body = _Body;
            this.CC = _CC;
            this.EMailTo = _EMailTo;
            this.Subject = _Subject;
        }
    }
}
